package dk.eclipse.android.badassapp.contact;

import android.app.Activity;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import dk.eclipse.android.badassapp.R;

/**
 * This class handles the construction of a contact row view (the visual contact list part on the screen).
 *   
 * @author Ronni Kahalani (eclipse.dk)
 *
 */
public class ContactArrayAdapter extends SimpleCursorAdapter {
	
	private final Activity context;

	/**
	 * Constructor.
	 * 
	 * @param context
	 * @param layout
	 * @param cursor
	 * @param columns
	 * @param names
	 */
	public ContactArrayAdapter(Activity context, int layout, Cursor cursor,
			String[] columns, int[] names) {
		super(context, layout, cursor, columns, names);
		this.context = context;
	}

	/* (non-Javadoc)
	 * @see android.widget.CursorAdapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.target_row_layout, null, true);
		} 

		Cursor cursor = getCursor();
		cursor.moveToPosition(position);

		TextView displayNameText = (TextView) rowView
				.findViewById(R.id.nameText);

		String displayName = cursor.getString(cursor
				.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
		displayNameText.setText(displayName);

		super.getView(position, rowView, parent);
		return rowView;
	}

}
